/**
 * @author GhostWei
 */
var index = {
	switchDrag: false,
	hh: 0,
	bindSwitch: function() {
		index.hb = $('#header').height() + 3;
		var _switch = $('#switch a');
		$('#switch').mousedown(function(e){
			index.switchDrag = true;
			$('body').css('cursor','e-resize');
			$('#maskDiv').css({'display': 'block'});
		});
		$(document).mousemove(function(e){
			if(!index.switchDrag) return;
			var x = e.clientX;
			$('#tree').css({"width" : x + 'px'});
			index.resize();
			$('#switch').css({'left': x+'px', 'top': index.hb+'px'});
			if(x <= 0) {
				_switch.attr('class', 'switch s1');
			} else {
				_switch.attr('class', 'switch');
			}
		});
		$(document).mouseup(function(e){
			index.switchDrag = false;
			$('body').css('cursor','default');
			$('#maskDiv').css('display','none');
			index.resize();
		});
		_switch.mousedown(function(){
			var w = 0;
			if($(this).attr('class') != 'switch') {
				w = 220;
				$(this).attr('class', 'switch');
			} else {
				$(this).attr('class', 'switch s1');
			}
			$('#tree').css({"width" : w + 'px'});
			$('#switch').css({'left': w + 'px'});
			index.resize();
		});
	},	
	resize: function() {
		var sourceHeight = $('#header').height() + 3;
		var bodyWidth = $('#body').width();
		var treeWidth = $('#tree').width();
		var switchWidth = $('#switch').width();
		var bodyHeight = $(window).height();
		
		var mainHeight = parseInt(bodyHeight - sourceHeight) + 'px';
		var frameWidth = parseInt(bodyWidth - (treeWidth + switchWidth));
		var scroll = frameWidth < 960 ? 'scroll' : 'hidden';
			
		$('#tree').css({"height" : mainHeight});
		$('#switch').css({"height" : mainHeight, 'left': treeWidth + 'px', 'top': sourceHeight + 'px'});		
		$('#frame').css({"width" : frameWidth + 'px', "height" : mainHeight});
		$('#maskDiv').css({'left': 0, 'top': 0, 'width': bodyWidth, 'height': bodyHeight});
		$('#mainFrame').css({"width" : "100%", "height" : mainHeight, "overflow-x" : scroll});
	}
};
$(window).resize(function(){index.resize();});
$(document).ready(function(){
	index.bindSwitch();
	index.resize();
	$('#menu>ul>li>ul').find('li:has(ul:not(:empty))>a').append('<span class="arrow"></span>');
	$("#menu>ul>li").bind('mouseover', function(){
		$(this).children('ul').slideDown('fast');
	}).bind('mouseleave', function(){
		$(this).children('ul').slideUp('fast');
	});
	$('#menu>ul>li>ul li').bind('mouseover', function(){
		$(this).children('ul').slideDown('fast');
	}).bind('mouseleave', function() {
		$(this).children('ul').slideUp('fast');
	});
});
